package class07;

import java.util.LinkedList;
import java.util.Stack;

/**
 * @description: 探索Java自带的Stack，用LinkedList实现的栈和用array实现的栈的性能
 * @time: 2021/7/16 14:52
 * @author: lhy
 */
public class TestStack {
    public static void main(String[] args) {
        long start;
        long end;
        int testTimes = 1000000;

        Stack<Integer> stack1 = new Stack<>();
        start = System.currentTimeMillis();
        for (int i = 0; i < testTimes; i++) {
            stack1.add(i);
        }
        while (!stack1.isEmpty()){
            stack1.pop();
        }
        end = System.currentTimeMillis();
        System.out.println("Java Stack:" + (end - start));

        LinkedList<Integer> stack2 = new LinkedList<>();
        start = System.currentTimeMillis();
        for (int i = 0; i < testTimes; i++) {
            stack2.addLast(i);
        }
        while (!stack2.isEmpty()){
            stack2.pollLast();
        }
        end = System.currentTimeMillis();
        System.out.println("LinkedList Stack:" + (end - start));

        int[] stack3 = new int[testTimes];
        int index = 0;
        start = System.currentTimeMillis();
        for (int i = 0; i < testTimes; i++) {
            stack3[index++] = i;
        }
        for (int i = 0; i < testTimes; i++) {
            int ans = stack3[--index];
        }
        end = System.currentTimeMillis();
        System.out.println("Array Stack:" + (end - start));
    }
}
